{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-01-01 140\n",
      "2021-01-02 175\n",
      "2021-01-03 192\n",
      "2021-01-04 188\n",
      "2021-01-05 163\n",
      "2021-01-06 177\n",
      "2021-01-07 109\n",
      "2021-01-08 149\n",
      "2021-01-09 145\n",
      "2021-01-10 155\n",
      "2021-01-11 153\n",
      "2021-01-12 147\n",
      "2021-01-13 160\n",
      "2021-01-14 155\n",
      "2021-01-15 161\n",
      "2021-01-16 158\n",
      "2021-01-17 157\n",
      "2021-01-18 161\n",
      "2021-01-19 146\n",
      "2021-01-20 150\n",
      "2021-01-21 166\n",
      "2021-01-22 169\n",
      "2021-01-23 166\n",
      "2021-01-24 183\n",
      "2021-01-25 176\n",
      "2021-01-26 166\n",
      "2021-01-27 159\n",
      "2021-01-28 159\n",
      "2021-01-29 149\n",
      "2021-01-30 161\n",
      "2021-01-31 162\n",
      "2021-02-01 181\n",
      "2021-02-02 176\n",
      "2021-02-03 183\n",
      "2021-02-04 178\n",
      "2021-02-05 171\n",
      "2021-02-06 184\n",
      "2021-02-07 176\n",
      "2021-02-08 171\n",
      "2021-02-09 179\n",
      "2021-02-10 161\n",
      "2021-02-11 165\n",
      "2021-02-12 149\n",
      "2021-02-13 157\n",
      "2021-02-14 155\n",
      "2021-02-15 137\n",
      "2021-02-16 159\n",
      "2021-02-17 167\n",
      "2021-02-18 172\n",
      "2021-02-19 168\n",
      "2021-02-20 172\n",
      "2021-02-21 163\n",
      "2021-02-22 140\n",
      "2021-02-23 160\n",
      "2021-02-24 167\n",
      "2021-02-25 142\n",
      "2021-02-26 138\n",
      "2021-02-27 115\n",
      "2021-02-28 147\n",
      "2021-03-01 154\n",
      "2021-03-02 160\n",
      "2021-03-03 175\n",
      "2021-03-04 173\n",
      "2021-03-05 165\n",
      "2021-03-06 170\n",
      "2021-03-07 168\n",
      "2021-03-08 178\n",
      "2021-03-09 163\n",
      "2021-03-10 169\n",
      "2021-03-11 163\n",
      "2021-03-12 152\n",
      "2021-03-13 159\n",
      "2021-03-14 160\n",
      "2021-03-15 160\n",
      "2021-03-16 155\n",
      "2021-03-17 182\n",
      "2021-03-18 181\n",
      "2021-03-19 176\n",
      "2021-03-20 187\n",
      "2021-03-21 171\n",
      "2021-03-22 192\n",
      "2021-03-23 155\n",
      "2021-03-24 169\n",
      "2021-03-25 148\n",
      "2021-03-26 154\n",
      "2021-03-27 169\n",
      "2021-03-28 130\n",
      "2021-03-29 155\n",
      "2021-03-30 141\n",
      "2021-03-31 148\n",
      "2021-04-01 154\n",
      "2021-04-02 145\n",
      "2021-04-03 141\n",
      "2021-04-04 156\n",
      "2021-04-05 152\n",
      "2021-04-06 133\n",
      "2021-04-07 149\n",
      "2021-04-08 157\n",
      "2021-04-09 158\n"
     ]
    },
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32mc:\\Users\\LDFriedrich\\Desktop\\百度指数.ipynb Cell 1\u001b[0m in \u001b[0;36m<cell line: 63>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     <a href='vscode-notebook-cell:/c%3A/Users/LDFriedrich/Desktop/%E7%99%BE%E5%BA%A6%E6%8C%87%E6%95%B0.ipynb#W0sZmlsZQ%3D%3D?line=64'>65</a>\u001b[0m start_date \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39m2021-01-01\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m     <a href='vscode-notebook-cell:/c%3A/Users/LDFriedrich/Desktop/%E7%99%BE%E5%BA%A6%E6%8C%87%E6%95%B0.ipynb#W0sZmlsZQ%3D%3D?line=65'>66</a>\u001b[0m end_date \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39m2022-11-20\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m---> <a href='vscode-notebook-cell:/c%3A/Users/LDFriedrich/Desktop/%E7%99%BE%E5%BA%A6%E6%8C%87%E6%95%B0.ipynb#W0sZmlsZQ%3D%3D?line=66'>67</a>\u001b[0m data \u001b[39m=\u001b[39m get_data(keyword, start_date, end_date)\n\u001b[0;32m     <a href='vscode-notebook-cell:/c%3A/Users/LDFriedrich/Desktop/%E7%99%BE%E5%BA%A6%E6%8C%87%E6%95%B0.ipynb#W0sZmlsZQ%3D%3D?line=67'>68</a>\u001b[0m \u001b[39m#url = \"https://index.baidu.com/api/SearchApi/index?area=917&word=[[%7B%22name%22:%22{}%22,%22wordType%22:1%7D]]&startDate={}&endDate={}\".format(keyword, start_date, end_date)\u001b[39;00m\n\u001b[0;32m     <a href='vscode-notebook-cell:/c%3A/Users/LDFriedrich/Desktop/%E7%99%BE%E5%BA%A6%E6%8C%87%E6%95%B0.ipynb#W0sZmlsZQ%3D%3D?line=68'>69</a>\u001b[0m \u001b[39m#text = get_data(url)\u001b[39;00m\n\u001b[0;32m     <a href='vscode-notebook-cell:/c%3A/Users/LDFriedrich/Desktop/%E7%99%BE%E5%BA%A6%E6%8C%87%E6%95%B0.ipynb#W0sZmlsZQ%3D%3D?line=69'>70</a>\u001b[0m \u001b[39m#print(data)\u001b[39;00m\n\u001b[0;32m     <a href='vscode-notebook-cell:/c%3A/Users/LDFriedrich/Desktop/%E7%99%BE%E5%BA%A6%E6%8C%87%E6%95%B0.ipynb#W0sZmlsZQ%3D%3D?line=70'>71</a>\u001b[0m \u001b[39mprint\u001b[39m(data)\n",
      "\u001b[1;32mc:\\Users\\LDFriedrich\\Desktop\\百度指数.ipynb Cell 1\u001b[0m in \u001b[0;36mget_data\u001b[1;34m(keyword, start, end)\u001b[0m\n\u001b[0;32m     <a href='vscode-notebook-cell:/c%3A/Users/LDFriedrich/Desktop/%E7%99%BE%E5%BA%A6%E6%8C%87%E6%95%B0.ipynb#W0sZmlsZQ%3D%3D?line=56'>57</a>\u001b[0m \u001b[39mfor\u001b[39;00m i \u001b[39min\u001b[39;00m \u001b[39mrange\u001b[39m(a\u001b[39m.\u001b[39mtoordinal(), b\u001b[39m.\u001b[39mtoordinal()):\n\u001b[0;32m     <a href='vscode-notebook-cell:/c%3A/Users/LDFriedrich/Desktop/%E7%99%BE%E5%BA%A6%E6%8C%87%E6%95%B0.ipynb#W0sZmlsZQ%3D%3D?line=57'>58</a>\u001b[0m     date \u001b[39m=\u001b[39m datetime\u001b[39m.\u001b[39mdate\u001b[39m.\u001b[39mfromordinal(i)\n\u001b[1;32m---> <a href='vscode-notebook-cell:/c%3A/Users/LDFriedrich/Desktop/%E7%99%BE%E5%BA%A6%E6%8C%87%E6%95%B0.ipynb#W0sZmlsZQ%3D%3D?line=58'>59</a>\u001b[0m     \u001b[39mprint\u001b[39m(date, result[node])\n\u001b[0;32m     <a href='vscode-notebook-cell:/c%3A/Users/LDFriedrich/Desktop/%E7%99%BE%E5%BA%A6%E6%8C%87%E6%95%B0.ipynb#W0sZmlsZQ%3D%3D?line=59'>60</a>\u001b[0m     node \u001b[39m+\u001b[39m\u001b[39m=\u001b[39m \u001b[39m1\u001b[39m\n",
      "\u001b[1;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "import datetime\n",
    "import requests\n",
    "import sys\n",
    "import time\n",
    "import json\n",
    "\n",
    "word_url = 'http://index.baidu.com/api/SearchApi/thumbnail?area=917&word={}'\n",
    "\n",
    "def get_html(url):\n",
    "    headers = {\n",
    "        \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36\",\n",
    "        \"Host\": \"index.baidu.com\",\n",
    "        \"Referer\": \"http://index.baidu.com/v2/main/index.html\",\n",
    "        \"Cipher-Text\": \"1669446300853_1669449849162_Zps8JEXzq9SKrsYVS8CTdXkWyVq/utJah5EChxPJNob8Fk+q4oEOdHsqjPHziaQdXUo3Soeq9UND9NJ7KI5474rTUgh6apQWleSKesxhLrG38d4HYhm3Z13QnTdY8SkltqngGgRMk7HZDt4ChGgwZwsbNYsvL1I9ur3MyF2msajKplNNj5Y3LMuaMS5gxyruyeErcZUV5UW9r2lxFRwMX9EnXK2ihb15TaGFqa6ByNiYNabljTXZeaPqTm2uBYONqcSHMuZAid7Lw10WAWKRxKymrNYz0fGm2tp5MtYdsmc+dLyP36RY62mTuLXOiVgwiZ4FfeCN9tThxh7q7ljdxAJQ4fa+oHrndNXwy+38TxWfRhxEr8lqpBUXXC1BG9vF\",\n",
    "        'Cookie':  'BIDUPSID=7175F15814438860CAFB14CF883E7713; PSTM=1651827154; MCITY=-%3A; BDUSS=F1SnppUURraUcwSnI2ZU5TM3dvOWNNeFdUcFFRRXg3VXF4ZFV3Y0Z1MUxDV2xqSVFBQUFBJCQAAAAAAQAAAAEAAADBav8mwrmx39Xf4q8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEt8QWNLfEFjb1; BAIDUID=70BB0450DEC3F9BA96830FB49D5B6777:FG=1; Hm_up_d101ea4d2a5c67dab98251f0b5de24dc=%7B%22uid_%22%3A%7B%22value%22%3A%224949240513%22%2C%22scope%22%3A1%7D%7D; Hm_lvt_d101ea4d2a5c67dab98251f0b5de24dc=1668771938,1669030492,1669034831,1669392407; bdindexid=poq2ghp1suh8dusg31kh7i7k75; SIGNIN_UC=70a2711cf1d3d9b1a82d2f87d633bd8a04194048577CctkN8scWLAOXMdkJeZ%2FiWixp2d1q%2Br44KqJRKwJa8KM74cbmYlHLiSd1tGRh1kXoBWUOmL5Ml0IxbYHmEbCzapgz2B2ILMFDrz6zXxRffi410xH4hdeEUSJuEUNsMU1rNKeUfHTuAUmAK%2Bqg0uF4vkH%2FFEpEbw6ItXuagGJ0yXhA2bqcjwOm9YA9HuSK8e0HNS5OhiJg9%2FLqa%2FMaV3rwoeKxWmTZ26nXrWLKBIWtk1V%2FHQnJL7G62MetFZuZRkvsl1ft0z3TWbkzsHE1dGBF0dBNRrVMULxXFvk3jRNJgVLAYDFGvk2K5P%2ByLzJaLVs45404352833234546797552929340337; __cas__rn__=419404857; __cas__st__212=6eb8e7171da38f246b33f23e4ee4270a3c84347a13d4952b1ac583d9f573dbde13a267189dd1fdeed4f2054e; __cas__id__212=43903776; CPID_212=43903776; CPTK_212=2076293730; H_PS_PSSID=; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BA_HECTOR=a0802l81008025a0a52g20mu1ho1u5q1e; delPer=0; PSINO=5; BAIDUID_BFESS=70BB0450DEC3F9BA96830FB49D5B6777:FG=1; ZFY=v3CS9rw2uLRxuUdY6t3NXUTv0J87jhdTUEtHTXbfIUc:C; RT=\"z=1&dm=baidu.com&si=34701a0f-e393-4af9-bad1-05286dbb19f3&ss=lawqqv3v&sl=z&tt=w5w&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf\"; Hm_lpvt_d101ea4d2a5c67dab98251f0b5de24dc=1669397347; ab_sr=1.0.1_NjU1NDIyZjUyMzIxYjg2MDZlZjFjN2M4NmRlNTE0MjdjNTI4MGIyN2MyYzg0Zjg3ODE3N2VkZjI1ODYzMzE4NWMwYWI2MGMwODE3YWE0NWZiYTY5YTJkM2QxNjNiM2ViM2VmYTEwNzgyYzlmNGU4MWYxM2JmYzVmODU5YmY4OGNiYjVhYjAzYjdhNWUzNDNkNDg5YzA4OTU3YjI1MjQ2Nw==; BDUSS_BFESS=F1SnppUURraUcwSnI2ZU5TM3dvOWNNeFdUcFFRRXg3VXF4ZFV3Y0Z1MUxDV2xqSVFBQUFBJCQAAAAAAQAAAAEAAADBav8mwrmx39Xf4q8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEt8QWNLfEFjb1'\n",
    "    }\n",
    "    \n",
    "    response = requests.get(url, headers=headers, )\n",
    "    return response.text\n",
    "\n",
    "\n",
    "def decrypt(t, e):\n",
    "    n = list(t)\n",
    "    i = list(e)\n",
    "    a = {}\n",
    "    result = []\n",
    "    ln = int(len(n) / 2)\n",
    "    start = n[ln:]\n",
    "    end = n[:ln]\n",
    "    for j, k in zip(start, end):\n",
    "        a.update({k: j})\n",
    "    for j in e:\n",
    "        result.append(a.get(j))\n",
    "    return ''.join(result)\n",
    "\n",
    "\n",
    "def get_ptbk(uniqid):\n",
    "    url = 'http://index.baidu.com/Interface/ptbk?uniqid={}'\n",
    "    resp = get_html(url.format(uniqid))\n",
    "    return json.loads(resp)['data']\n",
    "\n",
    "\n",
    "def get_data(keyword, start='2011-01-02', end='2022-01-02'):\n",
    "    url = \"https://index.baidu.com/api/SearchApi/index?area=917&word=[[%7B%22name%22:%22{}%22,%22wordType%22:1%7D]]&startDate={}&endDate={}\".format(keyword, start, end)\n",
    "    data = get_html(url)\n",
    "    data = json.loads(data)\n",
    "    uniqid = data['data']['uniqid']\n",
    "    data = data['data']['userIndexes'][0]['all']['data']\n",
    "    ptbk = get_ptbk(uniqid)\n",
    "    result = decrypt(ptbk, data)\n",
    "    result = result.split(',')\n",
    "    start = start_date.split(\"-\")\n",
    "    end = end_date.split(\"-\")\n",
    "    a = datetime.date(int(start[0]), int(start[1]), int(start[2]))\n",
    "    b = datetime.date(int(end[0]), int(end[1]), int(end[2]))\n",
    "    node = 0\n",
    "    for i in range(a.toordinal(), b.toordinal()):\n",
    "        date = datetime.date.fromordinal(i)\n",
    "        print(date, result[node])\n",
    "        node += 1\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    keyword = \"发热\"\n",
    "    start_date = \"2021-01-01\"\n",
    "    end_date = \"2022-11-20\"\n",
    "    data = get_data(keyword, start_date, end_date)\n",
    "    #url = \"https://index.baidu.com/api/SearchApi/index?area=917&word=[[%7B%22name%22:%22{}%22,%22wordType%22:1%7D]]&startDate={}&endDate={}\".format(keyword, start_date, end_date)\n",
    "    #text = get_data(url)\n",
    "    #print(data)\n",
    "    print(data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-04-10 153\n",
      "2022-04-11 192\n",
      "2022-04-12 191\n",
      "2022-04-13 179\n",
      "2022-04-14 174\n",
      "2022-04-15 152\n",
      "2022-04-16 160\n",
      "2022-04-17 140\n",
      "2022-04-18 146\n",
      "2022-04-19 173\n",
      "2022-04-20 165\n",
      "2022-04-21 186\n",
      "2022-04-22 176\n",
      "2022-04-23 161\n",
      "2022-04-24 161\n",
      "2022-04-25 164\n",
      "2022-04-26 169\n",
      "2022-04-27 189\n",
      "2022-04-28 163\n",
      "2022-04-29 150\n",
      "2022-04-30 162\n",
      "2022-05-01 158\n",
      "2022-05-02 152\n",
      "2022-05-03 153\n",
      "2022-05-04 150\n",
      "2022-05-05 156\n",
      "2022-05-06 106\n",
      "2022-05-07 167\n",
      "2022-05-08 166\n",
      "2022-05-09 165\n",
      "2022-05-10 162\n",
      "2022-05-11 161\n",
      "2022-05-12 170\n",
      "2022-05-13 150\n",
      "2022-05-14 165\n",
      "2022-05-15 140\n",
      "2022-05-16 163\n",
      "2022-05-17 113\n",
      "2022-05-18 181\n",
      "2022-05-19 167\n",
      "2022-05-20 170\n",
      "2022-05-21 157\n",
      "2022-05-22 159\n",
      "2022-05-23 173\n",
      "2022-05-24 174\n",
      "2022-05-25 167\n",
      "2022-05-26 167\n",
      "2022-05-27 175\n",
      "2022-05-28 113\n",
      "2022-05-29 154\n",
      "2022-05-30 169\n",
      "2022-05-31 99\n",
      "2022-06-01 161\n",
      "2022-06-02 171\n",
      "2022-06-03 170\n",
      "2022-06-04 156\n",
      "2022-06-05 156\n",
      "2022-06-06 170\n",
      "2022-06-07 198\n",
      "2022-06-08 203\n",
      "2022-06-09 185\n",
      "2022-06-10 156\n",
      "2022-06-11 165\n",
      "2022-06-12 179\n",
      "2022-06-13 185\n",
      "2022-06-14 184\n",
      "2022-06-15 177\n",
      "2022-06-16 192\n",
      "2022-06-17 182\n",
      "2022-06-18 163\n",
      "2022-06-19 172\n",
      "2022-06-20 194\n",
      "2022-06-21 167\n",
      "2022-06-22 166\n",
      "2022-06-23 181\n",
      "2022-06-24 176\n",
      "2022-06-25 183\n",
      "2022-06-26 177\n",
      "2022-06-27 184\n",
      "2022-06-28 213\n",
      "2022-06-29 185\n",
      "2022-06-30 199\n",
      "2022-07-01 189\n",
      "2022-07-02 163\n",
      "2022-07-03 184\n",
      "2022-07-04 179\n",
      "2022-07-05 178\n",
      "2022-07-06 174\n",
      "2022-07-07 169\n",
      "2022-07-08 164\n",
      "2022-07-09 172\n",
      "2022-07-10 171\n",
      "2022-07-11 166\n",
      "2022-07-12 186\n",
      "2022-07-13 195\n",
      "2022-07-14 188\n",
      "2022-07-15 212\n",
      "2022-07-16 189\n",
      "2022-07-17 197\n",
      "2022-07-18 178\n",
      "2022-07-19 172\n",
      "2022-07-20 100\n",
      "2022-07-21 187\n",
      "2022-07-22 109\n",
      "2022-07-23 169\n",
      "2022-07-24 169\n",
      "2022-07-25 181\n",
      "2022-07-26 185\n",
      "2022-07-27 182\n",
      "2022-07-28 150\n",
      "2022-07-29 174\n",
      "2022-07-30 172\n",
      "2022-07-31 164\n",
      "2022-08-01 156\n",
      "2022-08-02 181\n",
      "2022-08-03 156\n",
      "2022-08-04 162\n",
      "2022-08-05 163\n",
      "2022-08-06 155\n",
      "2022-08-07 96\n",
      "2022-08-08 121\n",
      "2022-08-09 167\n",
      "2022-08-10 168\n",
      "2022-08-11 155\n",
      "2022-08-12 100\n",
      "2022-08-13 171\n",
      "2022-08-14 154\n",
      "2022-08-15 161\n",
      "2022-08-16 175\n",
      "2022-08-17 165\n",
      "2022-08-18 159\n",
      "2022-08-19 184\n",
      "2022-08-20 176\n",
      "2022-08-21 155\n",
      "2022-08-22 166\n",
      "2022-08-23 161\n",
      "2022-08-24 93\n",
      "2022-08-25 166\n",
      "2022-08-26 165\n",
      "2022-08-27 92\n",
      "2022-08-28 89\n",
      "2022-08-29 144\n",
      "2022-08-30 181\n",
      "2022-08-31 160\n",
      "2022-09-01 148\n",
      "2022-09-02 151\n",
      "2022-09-03 153\n",
      "2022-09-04 148\n",
      "2022-09-05 147\n",
      "2022-09-06 148\n",
      "2022-09-07 144\n",
      "2022-09-08 154\n",
      "2022-09-09 160\n",
      "2022-09-10 156\n",
      "2022-09-11 86\n",
      "2022-09-12 136\n",
      "2022-09-13 166\n",
      "2022-09-14 156\n",
      "2022-09-15 143\n",
      "2022-09-16 154\n",
      "2022-09-17 161\n",
      "2022-09-18 106\n",
      "2022-09-19 150\n",
      "2022-09-20 164\n",
      "2022-09-21 153\n",
      "2022-09-22 154\n",
      "2022-09-23 160\n",
      "2022-09-24 137\n",
      "2022-09-25 159\n",
      "2022-09-26 152\n",
      "2022-09-27 173\n",
      "2022-09-28 148\n",
      "2022-09-29 159\n",
      "2022-09-30 157\n",
      "2022-10-01 154\n",
      "2022-10-02 76\n",
      "2022-10-03 146\n",
      "2022-10-04 150\n",
      "2022-10-05 159\n",
      "2022-10-06 89\n",
      "2022-10-07 156\n",
      "2022-10-08 139\n",
      "2022-10-09 148\n",
      "2022-10-10 143\n",
      "2022-10-11 143\n",
      "2022-10-12 159\n",
      "2022-10-13 159\n",
      "2022-10-14 146\n",
      "2022-10-15 163\n",
      "2022-10-16 163\n",
      "2022-10-17 157\n",
      "2022-10-18 166\n",
      "2022-10-19 161\n",
      "2022-10-20 170\n",
      "2022-10-21 160\n",
      "2022-10-22 174\n",
      "2022-10-23 145\n",
      "2022-10-24 89\n",
      "2022-10-25 156\n",
      "2022-10-26 149\n",
      "2022-10-27 150\n",
      "2022-10-28 148\n",
      "2022-10-29 99\n",
      "2022-10-30 151\n",
      "2022-10-31 76\n",
      "2022-11-01 163\n",
      "2022-11-02 152\n",
      "2022-11-03 154\n",
      "2022-11-04 158\n",
      "2022-11-05 165\n",
      "2022-11-06 156\n",
      "2022-11-07 96\n",
      "2022-11-08 154\n",
      "2022-11-09 166\n",
      "2022-11-10 162\n",
      "2022-11-11 143\n",
      "2022-11-12 155\n",
      "2022-11-13 163\n",
      "2022-11-14 154\n",
      "2022-11-15 168\n",
      "2022-11-16 165\n",
      "2022-11-17 150\n",
      "2022-11-18 154\n",
      "2022-11-19 148\n"
     ]
    }
   ],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "# @time: 2022/1/4 8:35\n",
    "# @Author: 韩国麦当劳\n",
    "# @Environment: Python 3.7\n",
    "import datetime\n",
    "import requests\n",
    "import sys\n",
    "import time\n",
    "import json\n",
    "\n",
    "word_url = 'http://index.baidu.com/api/SearchApi/thumbnail?area=0&word={}'\n",
    "\n",
    "def get_html(url):\n",
    "    headers = {\n",
    "        \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36\",\n",
    "        \"Host\": \"index.baidu.com\",\n",
    "        \"Referer\": \"http://index.baidu.com/v2/main/index.html\",\n",
    "        \"Cipher-Text\": \"1652425237825_1652501356206_VBpwl9UG8Dvs2fAi91KToRTSAP7sDsQU5phHL97raPDFJdYz3fHf9hBAQrGGCs+qJoP7yb44Uvf91F7vqJLVL0tKnIWE+W3jXAI30xx340rhcwUDQZ162FPAe0a1jsCluJRmMLZtiIplubGMW/QoE/0Pw+2caH39Ok8IsudE4wGLBUdYg1/bKl4MGwLrJZ7H6wbhR0vT5X0OdCX4bMJE7vcwRCSGquRjam03pWDGZ51X15fOlO0qMZ2kqa3BmxwNlfEZ81l3L9nZdrc3/Tl4+mNpaLM7vA5WNEQhTBoDVZs6GBRcJc/FSjd6e4aFGAiCp1Y8MD66chTiykjIN51s7gbJ44JfVS0NjBnsvuF55bs=\"\n",
    "    }\n",
    "    cookies = {\n",
    "        'Cookie': 'BIDUPSID=7175F15814438860CAFB14CF883E7713; PSTM=1651827154; MCITY=-%3A; BDUSS=F1SnppUURraUcwSnI2ZU5TM3dvOWNNeFdUcFFRRXg3VXF4ZFV3Y0Z1MUxDV2xqSVFBQUFBJCQAAAAAAQAAAAEAAADBav8mwrmx39Xf4q8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEt8QWNLfEFjb1; BAIDUID=70BB0450DEC3F9BA96830FB49D5B6777:FG=1; Hm_up_d101ea4d2a5c67dab98251f0b5de24dc=%7B%22uid_%22%3A%7B%22value%22%3A%224949240513%22%2C%22scope%22%3A1%7D%7D; Hm_lvt_d101ea4d2a5c67dab98251f0b5de24dc=1668771938,1669030492,1669034831,1669392407; bdindexid=poq2ghp1suh8dusg31kh7i7k75; SIGNIN_UC=70a2711cf1d3d9b1a82d2f87d633bd8a04194048577CctkN8scWLAOXMdkJeZ%2FiWixp2d1q%2Br44KqJRKwJa8KM74cbmYlHLiSd1tGRh1kXoBWUOmL5Ml0IxbYHmEbCzapgz2B2ILMFDrz6zXxRffi410xH4hdeEUSJuEUNsMU1rNKeUfHTuAUmAK%2Bqg0uF4vkH%2FFEpEbw6ItXuagGJ0yXhA2bqcjwOm9YA9HuSK8e0HNS5OhiJg9%2FLqa%2FMaV3rwoeKxWmTZ26nXrWLKBIWtk1V%2FHQnJL7G62MetFZuZRkvsl1ft0z3TWbkzsHE1dGBF0dBNRrVMULxXFvk3jRNJgVLAYDFGvk2K5P%2ByLzJaLVs45404352833234546797552929340337; __cas__rn__=419404857; __cas__st__212=6eb8e7171da38f246b33f23e4ee4270a3c84347a13d4952b1ac583d9f573dbde13a267189dd1fdeed4f2054e; __cas__id__212=43903776; CPID_212=43903776; CPTK_212=2076293730; H_PS_PSSID=; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BA_HECTOR=a0802l81008025a0a52g20mu1ho1u5q1e; delPer=0; PSINO=5; BAIDUID_BFESS=70BB0450DEC3F9BA96830FB49D5B6777:FG=1; ZFY=v3CS9rw2uLRxuUdY6t3NXUTv0J87jhdTUEtHTXbfIUc:C; RT=\"z=1&dm=baidu.com&si=34701a0f-e393-4af9-bad1-05286dbb19f3&ss=lawqqv3v&sl=z&tt=w5w&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf\"; Hm_lpvt_d101ea4d2a5c67dab98251f0b5de24dc=1669397347; ab_sr=1.0.1_NjU1NDIyZjUyMzIxYjg2MDZlZjFjN2M4NmRlNTE0MjdjNTI4MGIyN2MyYzg0Zjg3ODE3N2VkZjI1ODYzMzE4NWMwYWI2MGMwODE3YWE0NWZiYTY5YTJkM2QxNjNiM2ViM2VmYTEwNzgyYzlmNGU4MWYxM2JmYzVmODU5YmY4OGNiYjVhYjAzYjdhNWUzNDNkNDg5YzA4OTU3YjI1MjQ2Nw==; BDUSS_BFESS=F1SnppUURraUcwSnI2ZU5TM3dvOWNNeFdUcFFRRXg3VXF4ZFV3Y0Z1MUxDV2xqSVFBQUFBJCQAAAAAAQAAAAEAAADBav8mwrmx39Xf4q8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEt8QWNLfEFjb1'\n",
    "    }\n",
    "    response = requests.get(url, headers=headers, cookies=cookies)\n",
    "    return response.text\n",
    "\n",
    "\n",
    "def decrypt(t, e):\n",
    "    n = list(t)\n",
    "    i = list(e)\n",
    "    a = {}\n",
    "    result = []\n",
    "    ln = int(len(n) / 2)\n",
    "    start = n[ln:]\n",
    "    end = n[:ln]\n",
    "    for j, k in zip(start, end):\n",
    "        a.update({k: j})\n",
    "    for j in e:\n",
    "        result.append(a.get(j))\n",
    "    return ''.join(result)\n",
    "\n",
    "\n",
    "def get_ptbk(uniqid):\n",
    "    url = 'http://index.baidu.com/Interface/ptbk?uniqid={}'\n",
    "    resp = get_html(url.format(uniqid))\n",
    "    return json.loads(resp)['data']\n",
    "\n",
    "\n",
    "def get_data(keyword, start='2011-01-02', end='2022-01-02'):\n",
    "    url = \"https://index.baidu.com/api/SearchApi/index?area=917&word=[[%7B%22name%22:%22{}%22,%22wordType%22:1%7D]]&startDate={}&endDate={}\".format(keyword, start, end)\n",
    "    data = get_html(url)\n",
    "    data = json.loads(data)\n",
    "    uniqid = data['data']['uniqid']\n",
    "    data = data['data']['userIndexes'][0]['all']['data']\n",
    "    ptbk = get_ptbk(uniqid)\n",
    "    result = decrypt(ptbk, data)\n",
    "    result = result.split(',')\n",
    "    start = start_date.split(\"-\")\n",
    "    end = end_date.split(\"-\")\n",
    "    a = datetime.date(int(start[0]), int(start[1]), int(start[2]))\n",
    "    b = datetime.date(int(end[0]), int(end[1]), int(end[2]))\n",
    "    node = -1\n",
    "    for i in range(a.toordinal(), b.toordinal()):\n",
    "        date = datetime.date.fromordinal(i)\n",
    "        print(date, result[node])\n",
    "        node += 1\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    keyword = \"发热\"\n",
    "    start_date = \"2022-04-10\"\n",
    "    end_date = \"2022-11-20\"\n",
    "    get_data(keyword, start_date, end_date)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.10 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "ab1d930aa3d38ca1f2d3995a6e83c91a45283867d558d974f0694a0f6727de6f"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
